<template>
  <a-modal
    v-model:visible="visible"
    title="新增标签信息"
    :maskClosable="false"
    @ok="handleOk"
    :style="{
      top: '0px',
      width: winWidth + 'px',
      height: 'auto',
    }"
  >
    <a-card
      :body-style="{ padding: '24px 32px' }"
      :bordered="false"
      :style="{
        top: '0px',
        width: '100%',
        height: winHeight + 'px',
        overflow: 'hidden',
        overflowY: 'scroll',
      }"
    >
      <a-form
        ref="formRef"
        name="basic"
        :label-col="{ span: 4 }"
        :wrapper-col="{ span: 18 }"
        autocomplete="off"
      >
        <a-form-item label="图标" name="tagLabel">
          <Avatar ref="avatar" :data="formValue.icon" />
        </a-form-item>
        <a-form-item
          label="标签"
          name="tagLabel"
          :rules="[{ required: true, message: '' }]"
        >
          <a-input v-model:value="formValue.tagLabel" />
        </a-form-item>

        <a-form-item
          label="类型"
          name="msgType"
          :rules="[{ required: true, message: '' }]"
        >
          <a-select
            ref="select"
            v-model:value="formValue.groupKey"
            style="width: 120px"
          >
            <a-select-option
              v-for="(item, idx) in groupKeyOpts"
              :key="idx"
              :value="item.value"
              >{{ item.label }}</a-select-option
            >
          </a-select>
        </a-form-item>

        <a-form-item
          label="状态"
          name="status"
          :rules="[{ required: true, message: '' }]"
        >
          <a-select
            ref="select"
            v-model:value="formValue.status"
            style="width: 120px"
            @change="handleStatusChange"
          >
            <a-select-option
              v-for="item in statusOpts"
              :key="item.value"
              :value="item.value"
              >{{ item.label }}</a-select-option
            >
          </a-select>
        </a-form-item>
      </a-form>
    </a-card>
    <template #footer>
      <a-button key="submit" type="primary" :loading="loading" @click="handleOk"
        >提交</a-button
      >
      <a-button key="back" @click="handleCancel">返回</a-button>
    </template>
  </a-modal>
</template>
<script lang="ts">
import {
  defineComponent,
  ref,
  reactive,
  onMounted,
  onBeforeUnmount,
  defineEmits,
} from "vue";
import { FormValue, submit, statusOptions, getTagGroupKeys } from "./index";
import ResultEnum from "@/config/ResultEnum";
import { message } from "ant-design-vue";
import { copyObjVal } from "@/utils/common";
import { PlusOutlined } from "@ant-design/icons-vue";
import Avatar from "@/components/AvatarUpload/index.vue";

export default defineComponent({
  components: {
    PlusOutlined,
    Avatar,
  },
  setup(props, context) {
    const emit = defineEmits(["refreshTable"]);
    const formRef = ref();
    const visible = ref(false);
    const loading = ref(false);
    const winWidth = ref(500);
    const winHeight = ref(350);
    const avatar = ref();

    const statusOpts = ref(statusOptions);

    const groupKeyOpts = ref([] as any);

    const defaultValues = () => {
      return {
        tagLabel: "",
        groupKey: "",
        status: "Normal",
        icon: null,
      };
    };

    const formValue = reactive<FormValue>(defaultValues());

    const initData = () => {
      let defaultVals = defaultValues();
      copyObjVal(defaultVals, formValue);
      getTagGroupKeys().then((res: any) => {
        groupKeyOpts.value = res;
      });
      avatar.value.setData(null);
    };

    const display = () => {
      visible.value = true;
      initData();
    };

    //确定按钮点击事件
    const handleOk = () => {
      validateParams()
        .then((res) => {
          loading.value = true;
          submit({
            tagLabel: formValue.tagLabel,
            groupKey: formValue.groupKey,
            status: formValue.status,
            icon: avatar.value.getData(),
            tagKey: "",
          }).then((res: any) => {
            loading.value = false;
            if (res.code == ResultEnum.SUCCESS_CODE) {
              message.success("提交成功");
              visible.value = false;
              context.emit("refreshTable");
            } else {
              message.error(res.message);
            }
          });
        })
        .catch((res: string) => {
          message.error(res);
        });
    };

    //校验数据
    const validateParams = () => {
      return new Promise((resolve: any, reject: any) => {
        if (!formValue.tagLabel) {
          reject("请填写标签");
        }
        if (!formValue.groupKey) {
          reject("请选择类型");
        }
        if (!formValue.status) {
          reject("请选择状态");
        }
        resolve("ok");
      });
    };

    const handleCancel = () => {
      //formValue.content = "";
      console.log(defaultValues);
      visible.value = false;
    };

    onMounted(() => {
      window.onresize = () => {
        winWidth.value = document.body.clientWidth / 2;
        winHeight.value = document.body.clientHeight - 200;
      };
    });

    const handleStatusChange = (value: any) => {
      console.log(value);
    };

    onBeforeUnmount(() => {});

    //返回数据
    return {
      visible,
      display,
      handleOk,
      handleCancel,
      loading,
      formValue,
      winWidth,
      winHeight,
      handleStatusChange,
      statusOpts,
      groupKeyOpts,
      PlusOutlined,
      avatar,
    };
  },
});
</script>
